var express = require('express');
var router = express.Router();
let md5 = require('md5');
let config = require('../config/config');
let {User, Session, Article} = require('../database/sequelize');
let {isEmail, isPhone} = require('../utils/utils');


/**
 * 获取文章列表列表，分页
 */
router.get('/bms/article/list', async function (req, res, next) {

  let page = req.body.page;
  let order = req.body.order;
  let sortBy = req.body.sortBy;
  let limit = req.body.limit;

  //过滤
  if (!page || page < 1 || isNaN(page)) {
    page = 1
  }

  if (order !== 'desc' && order !== 'asc') {
    order = 'desc';
  }

  if (sortBy) {
    sortBy = "id"
  }

  if (!limit || limit < 1 || isNaN(limit)) {
    limit = 10;
  }
  let data = [];
//order: [[sortBy, order]]

  await Article.findAll({offset: (page - 1) * limit, limit: limit})
    .then(article => {
      for (let item of article) {
        data.push(item.dataValues);
      }
    });


  if (data) {
    res.send({"success": true, "msg": "success", data: data});
  } else {
    res.send({"success": false, "msg": "失败"});
  }


});


/**
 * 获取文章详情信息
 */
router.get('/bms/article/detail', async function (req, res, next) {

  let id = req.body.id;

  //过滤
  if (!id || isNaN(id)) {
    res.send({"success": false, "msg": "参数错误"});
    return
  }
  let data = [];

  await Article.findById(id)
    .then(article => {
      data = article.dataValues;

    });

  if (data) {
    res.send({"success": true, "msg": "success", data: data});
  } else {
    res.send({"success": false, "msg": "失败"});
  }


});


/**
 * 添加文章信息
 */
router.post('/bms/article/add', async function (req, res, next) {

  let title = req.body.title;
  let sub_title = req.body.sub_title;
  let title_image = req.body.title_image;
  let abstract = req.body.abstract;
  let content = req.body.content;
  let user_id = req.body.uid;

  //过滤
  if (!title || !content || !user_id || isNaN(user_id)) {
    res.send({"success": false, "msg": "参数错误"});
    return
  }


  let data = [];


  await Article.create({
    id: null,
    title: title,
    sub_title: sub_title,
    title_image: title_image,
    abstract: abstract,
    content: content,
    user_id: user_id,
    page_view: 0,
    updated_at: new Date().getTime() / 1000,
    created_at: new Date().getTime() / 1000
  }).then(article => {
    data = article.dataValues;
  });


  if (data) {
    res.send({"success": true, "msg": "success", data: data});
  } else {
    res.send({"success": false, "msg": "失败"});
  }

});


/**
 * 更新文章信息
 */
router.post('/bms/article/update', async function (req, res, next) {

  let id = req.body.id;
  let title = req.body.title;
  let sub_title = req.body.sub_title;
  let title_image = req.body.title_image;
  let abstract = req.body.abstract;
  let content = req.body.content;

  //过滤
  if (!id || isNaN(id) || !title || !content || !user_id || isNaN(user_id)) {
    res.send({"success": false, "msg": "参数错误"});
    return
  }

  let data = [];

  await Article.update({
    title: title,
    sub_title: sub_title,
    title_image: title_image,
    abstract: abstract,
    content: content,
    updated_at: new Date().getTime() / 1000,
  }, {
    where: {id: id}
  }).then(article => {
    data = article[0];
  });

  if (data) {
    res.send({"success": true, "msg": "success", data: data});
  } else {
    res.send({"success": false, "msg": "失败"});
  }

});


/**
 * 删除文章记录
 */
router.post('/bms/article/delete', async function (req, res, next) {

  let id = req.body.id;
  //过滤
  if (!id || isNaN(id)) {
    res.send({"success": false, "msg": "参数错误"});
    return
  }


  let data = {};

  await Article.destroy({
    where: {id: id}
  }).then(article => {
    data = article;
  });

  if (data) {
    res.send({"success": true, "msg": "success", data: data});
  } else {
    res.send({"success": false, "msg": "失败"});
  }

});




module.exports = router;
